package com.titanrise.crm.controller.root;

import cn.hutool.core.util.NumberUtil;
import com.titanrise.crm.common.enums.LogEffect;
import com.titanrise.crm.common.enums.TradeStatusEnum;
import com.titanrise.crm.common.utils.*;
import com.titanrise.crm.domain.dto.TradeDto;
import com.titanrise.crm.domain.dto.UserDto;
import com.titanrise.crm.domain.model.SysLoginLog;
import com.titanrise.crm.environment.annotation.Autowired;
import com.titanrise.crm.environment.annotation.Controller;
import com.titanrise.crm.environment.annotation.FunctionConfig;
import com.titanrise.crm.environment.menu.action.Action;
import com.titanrise.crm.exception.InputFormatErrorException;
import com.titanrise.crm.handler.ArrowKeyPagination;
import com.titanrise.crm.handler.SharedData;
import com.titanrise.crm.service.RootService;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: pront
 * @Time:2024-11-13 17:33
 */
@Controller(basePath = "/root")
public class RootIndexController {

    @Autowired
    private RootService rootService;


    private List<SysLoginLog> sysLoginLogs = new ArrayList<>();

    @FunctionConfig(path = "/", key = 1, description = "管理账户信息")
    public Action showAllMembers() {
        List<UserDto> list = rootService.showAllMembers();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("当前查询 " + list.size() + " 条记录");
        System.out.println("|  序号  |  姓名  |  手机号码  |  登录账号  |  登录密码  |  年收入  |");

        int idx = 1;
        for (UserDto user : list) {
            System.out.println("  |  " + (idx++) + "  |  " + user.getName() + " | " + user.getPhoneNumber() +
                    " | " + user.getUsername() + " | " + user.getPassword() + "  |  " + user.getAnnualIncome() + "  |");
        }
        return Action.JUMP_TO_MENU("/root/api/manageMemberInfo");
    }

    @FunctionConfig(path = "/", key = 2, description = "管理交易信息")
    private Action showAllTrades() {
        List<TradeDto> tradeList = rootService.showAllTrades();
        System.out.println("共查询到" + tradeList.size() + "条记录");
        System.out.println("|  序号  |  销售人员姓名  |  客户姓名  |  商品名称  |  商品价格  |  更新日期  |  交易状态  |  数量  |  备注  |");
        tradeList = tradeList.stream().peek(trade -> {
            for (TradeStatusEnum status : TradeStatusEnum.values()) {
                if (status.getStatus() == trade.getStatus()) {
                    trade.setStatusMsg(status.getMsg());
                    break;
                }
            }
        }).collect(Collectors.toList());
        int idx = 1;
        for (TradeDto trade : tradeList) {
            System.out.println("  |  " + (idx++) + "  |  " + trade.getSalespersonName() + " | " + trade.getClientName() +
                    " | " + trade.getGoodsName() + " | " + trade.getPrice() + "  |  " + trade.getUpdateTime() + "  |  "
                    + trade.getStatusMsg() + "  |  " + trade.getNumber() + "  |  " + trade.getNumber());
        }
        return Action.JUMP_TO_MENU("/root/api/manageTradeInfo");
    }

    @FunctionConfig(path = "/", key = 3, description = "管理客户所属关系", divider = "")
    public Action manageClientInfo(){
        return Action.JUMP_TO_MENU("/root/api/manageClientInfo");
    }

    @FunctionConfig(path = "/", key = 4, description = "查看系统登录日志")
    public Action sysLoginLogIO(){
        try {
            Function<String, Either<String, String>> checker = Functions.flow(
                    ValidationUtil::trimThenRight,
                    ValidationUtil.nonNull("输入不可为空！请重试！")
            );
            String tl = ProjectUtil.getLine("请输入查询时限(？)天:", checker);
            if (!NumberUtil.isInteger(tl)) throw new InputFormatErrorException();
//          从第一页开始查询
            sysLoginLogImpl(Integer.parseInt(tl) , 1);
        } catch (InputFormatErrorException e) {
            Console.println(e.getMessage(), LogEffect.RED, LogEffect.BOLD);
            return Action.JUMP_TO_FUNCTION("/root", 4);
        }
        return Action.JUMP_TO_MENU("/root");
    }

    @FunctionConfig(path = "/", key = 5, description = "登出管理员系统")
    public Action exitRootPage(){
        return Action.JUMP_TO_MENU("/");
    }

    @FunctionConfig(path = "/sysLoginLog", key = 1, description = "查看系统登录日志")
    public Action sysLoginLogImpl(int timeLine, int page){
        SharedData sharedData = new SharedData();
        sharedData.setCurrentPage(page);
        ArrowKeyPagination arrowKeyPagination = new ArrowKeyPagination(sharedData, timeLine);
        // 启动分页监听
        arrowKeyPagination.startPagination();
        return Action.JUMP_TO_MENU("/root");
    }
}
